package org.bilinc.gz001;

import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.util.Enumeration;
import java.util.Vector;

public class GroupableTableHeader extends JTableHeader {
    private static final String uiClassID = "GroupableTableHeaderUI";
    private Vector mColumnGroups;

    public GroupableTableHeader(final TableColumnModel model) {
        super(model);
        setUI(new GroupableTableHeaderUI());
        setReorderingAllowed(false);
    }

    public void updateUI() {
        setUI(new GroupableTableHeaderUI());
    }

    public void setReorderingAllowed(final boolean b) {
        reorderingAllowed = false;
    }

    public void addColumnGroup(final ColumnGroup g) {
        if (mColumnGroups == null) {
            mColumnGroups = new Vector();
        }
        mColumnGroups.addElement(g);
    }

    public Enumeration getColumnGroups(final TableColumn col) {
        if (mColumnGroups == null) {
            return null;
        }

        final Enumeration e = mColumnGroups.elements();
        while (e.hasMoreElements()) {
            final ColumnGroup cGroup = (ColumnGroup) e.nextElement();
            final Vector v_ret = (Vector) cGroup.getColumnGroups(col, new Vector());

            if (v_ret != null) {
                return v_ret.elements();
            }
        }
        return null;
    }

    public void setColumnMargin() {
        if (mColumnGroups == null) {
            return;
        }

        final int columnMargin = getColumnModel().getColumnMargin();
        final Enumeration e = mColumnGroups.elements();
        while (e.hasMoreElements()) {
            final ColumnGroup cGroup = (ColumnGroup) e.nextElement();
            cGroup.setColumnMargin(columnMargin);
        }
    }

}